package com.pig4cloud.pig.module.controller.property;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.property.PropertyRegister;
import com.pig4cloud.pig.module.service.property.IPropertyRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 物业入驻申请表Controller
 *
 * @author ruoyi
 * @date 2022-10-18
 */
@Validated
@Api(value = "物业入驻申请表控制器", tags = {"物业入驻申请表管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/property/register")
public class PropertyRegisterController {

    private final IPropertyRegisterService propertyRegisterService;

    /**
     * 查询物业入驻申请表列表
     */
    @ApiOperation("查询物业入驻申请表列表")
    @PreAuthorize("@pms.hasPermission('module:propertyReg:list')")
    @GetMapping("/list")
    public TableDataInfo<PropertyRegister> list(@Validated PropertyRegister propertyRegister) {
        QueryWrapper<PropertyRegister> qw = new QueryWrapper<>();

        TableDataInfo<PropertyRegister> td = PageUtils.buildDataInfo(propertyRegisterService.page(PageUtils.buildPage(),qw));
        List<PropertyRegister> list = td.getData();
        return td;
    }

    /**
     * 导出物业入驻申请表列表
     */
    @ApiOperation("导出物业入驻申请表列表")
    @PreAuthorize("@pms.hasPermission('module:propertyReg:export')")
    @SysLog("物业入驻申请表")
    @GetMapping("/export")
    public List export(@Validated PropertyRegister propertyRegister) {
        List<PropertyRegister> list = propertyRegisterService.list();
        // ExcelUtil<PropertyRegister> util = new // ExcelUtil<PropertyRegister>(PropertyRegister.class);
        return  list;// util.exportExcel(list, "物业入驻申请表");
    }

    /**
     * 获取物业入驻申请表详细信息
     */
    @ApiOperation("获取物业入驻申请表详细信息")
    @PreAuthorize("@pms.hasPermission('module:propertyReg:query')")
    @GetMapping("/{id}")
    public R<PropertyRegister> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PropertyRegister propertyRegister = propertyRegisterService.getById(id);
        return R.ok(propertyRegister);
    }

    /**
     * 新增物业入驻申请表
     */
    @ApiOperation("新增物业入驻申请表")
    @PreAuthorize("@pms.hasPermission('module:propertyReg:add')")
    @SysLog("物业入驻申请表")
    @PostMapping()
    public R<Void> add(@Valid @RequestBody PropertyRegister propertyRegister) {
        return R.okByRow(propertyRegisterService.save(propertyRegister) ? 1 : 0);
    }

    /**
     * 修改物业入驻申请表
     */
    @ApiOperation("修改物业入驻申请表")
    @PreAuthorize("@pms.hasPermission('module:propertyReg:edit')")
    @SysLog("物业入驻申请表")
    @PutMapping()
    public R<Void> edit(@Valid @RequestBody PropertyRegister propertyRegister) {
        return R.okByRow(propertyRegisterService.updateById(propertyRegister) ? 1 : 0);
    }

    /**
     * 删除物业入驻申请表
     */
    @ApiOperation("删除物业入驻申请表")
    @PreAuthorize("@pms.hasPermission('module:propertyReg:remove')")
    @SysLog("物业入驻申请表")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(propertyRegisterService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
